#include "smart_byte_array.h"

SmartByteArray::SmartByteArray(const QByteArray &array) : _byteArray(array)
{
}

QList<QByteArray> SmartByteArray::split(const char* sep, int size, unsigned int rounds) const
{
    unsigned int r_counter = 0;
    int likeness = 0;
    int start = 0;
    QList<QByteArray> array;
    for (int i=0; i<_byteArray.length(); i++)
    {
        if(r_counter == rounds)
        {
            QByteArray temp = "";
            for(; start<=_byteArray.length()-size; start++)
                temp+=_byteArray[start];
            array.append(temp);
            break;
        }
        if (_byteArray[i] == sep[likeness])
        {
            likeness++;
        }
        else likeness = 0;
        if(i==_byteArray.length()-1)
        {
            QByteArray temp = "";
            for(; start<=i-size+1; start++)
                temp+=_byteArray[start];
            array.append(temp);
            start++;
        }
        else
        if (likeness == size)
        {
            QByteArray temp = "";
            for(;start<=i-size; start++)
                temp+=_byteArray[start];
            array.append(temp);
            start+=size;
            r_counter++;
        }
    }
    return array;
}

QByteArray SmartByteArray::data() const
{
    return _byteArray;
}
